package com.bawei.property

import android.graphics.BitmapFactory
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.RouterPath
import com.bawei.common.mvi.BaseMviActivity
import com.bawei.property.databinding.ActivityParkDetailBinding
import com.bawei.property.entity.CarApplyValues
import com.google.vr.sdk.widgets.pano.VrPanoramaEventListener
import com.google.vr.sdk.widgets.pano.VrPanoramaView
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

/**
 * 车位详情
 * VR预览车位周边环境
 */

@AndroidEntryPoint
@Route(path = RouterPath.PATH_PARK_DETAIL)
class ParkDetailActivity : BaseMviActivity<ActivityParkDetailBinding>() {

    @Autowired
    lateinit var carApplyEntity: CarApplyValues

    override fun getLayoutId(): Int = R.layout.activity_park_detail
    override fun initViews() {
        ARouter.getInstance().inject(this)
        mDataBinding.includedTitle.tvTitle.text = "车位预览"
        mDataBinding.includedTitle.ivBack.setOnClickListener {
            this@ParkDetailActivity.finish()
        }
        mDataBinding.tvSelect.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_PARK_SELECT).withSerializable("carApplyEntity", carApplyEntity).navigation()
        }

        //设置预览参数
        mDataBinding.vrPanoramaView.setInfoButtonEnabled(false) //删除不需要连接
        mDataBinding.vrPanoramaView.setFullscreenButtonEnabled(false) //隐藏全屏按钮
        //设置VR加载事件监听
        mDataBinding.vrPanoramaView.setEventListener(object : VrPanoramaEventListener(){
            override fun onLoadError(errorMessage: String) {
                Log.i("======", "Vr onLoadError: $errorMessage")

            }
            override fun onLoadSuccess() {
                Log.i("======", "Vr onLoadSuccess: ")

            }
        })

        //加载车位全景图片
        loadImageFromAssets()
    }

    /**
     * 从Assets中加载图片
     */
    private fun loadImageFromAssets(){
        lifecycleScope.launch(Dispatchers.IO) {
            try {
                var assetsManager = assets
                var inputStream = assetsManager.open("cc.jpg")
                var bitmap = BitmapFactory.decodeStream(inputStream)
                var pannoOptions = VrPanoramaView.Options()
                pannoOptions.inputType = VrPanoramaView.Options.TYPE_MONO //图片类型VR预览
                mDataBinding.vrPanoramaView.loadImageFromBitmap(bitmap, pannoOptions)
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                cancel()
            }
        }
    }

    override fun onResume() {
        mDataBinding.vrPanoramaView.resumeRendering() //恢复渲染
        super.onResume()
    }

    override fun onPause() {
        mDataBinding.vrPanoramaView.pauseRendering() //暂停渲染
        super.onPause()
    }


    override fun onDestroy() {
        mDataBinding.vrPanoramaView.shutdown() //关闭渲染
        super.onDestroy()
    }


}